package com.duoketech.config;

import com.duoketech.routes.FrontRoutes;
import com.duoketech.routes.WeiXinRoutes;
import com.duoketech.utils.DbUtil;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.redis.RedisPlugin;
import com.jfinal.template.Engine;
import com.jfinal.weixin.sdk.api.ApiConfig;
import com.jfinal.weixin.sdk.api.ApiConfigKit;

public class ProjectConfig extends JFinalConfig {

	public static void main(String[] args) {

		JFinal.start("src/main/webapp", 8181, "/",5);
    }
	
	public void configRoute(Routes me) {

		me.add(new FrontRoutes());
		me.add(new WeiXinRoutes());
	}
	
	public void configEngine(Engine me) {
		me.setDevMode(true);
		me.addSharedFunction("/WEB-INF/view/front/common/__layout.html");
		me.addSharedFunction("/WEB-INF/view/front/common/__wx_layout.html");
		me.addSharedFunction("/WEB-INF/view/front/common/macro.html");

		me.addSharedObject("redirectUrl",Constant.WX_REDIRECT_URL);

	}
	
	public void configConstant(Constants me) {
		PropKit.use("a_little_config.txt");
		me.setDevMode(true);
		ApiConfigKit.setDevMode(me.getDevMode());
	}
	public void configPlugin(Plugins me) {
		DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbcUrl"),
				PropKit.get("user"), PropKit.get("password").trim());
		ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
		arp.setShowSql(true);
		DbUtil.mappingModel(arp);
		me.add(druidPlugin);
		me.add(arp);
		RedisPlugin redisPlugin = new RedisPlugin(DbUtil.REDIS_CACHE_TRAVEL,"127.0.0.1");
		me.add(redisPlugin);
	}
	public void configInterceptor(Interceptors me) {}
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("contextPath"));// 设置上下文路路径 防止样式丢失
	}

	public void afterJFinalStart() {
		ApiConfig ac = new ApiConfig();
		ac.setToken(Constant.WX_TOKEN);
		ac.setAppId(Constant.WX_APPID);
		ac.setAppSecret(Constant.WX_SECRET);
		ApiConfigKit.putApiConfig(ac);
	}
}
